﻿// <copyright file="TileImageCollection.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>9/19/2009</date>
// <summary>Collection of images for tiles.</summary>

// License:

// Product: ASCII_GDI.csproj
// Author: Robert N. Tenney
// Copyright: 9/19/2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace TenneySoftware.GraphicsManagers {
    /// <summary>
    /// TileImageCollection contains the list of tiles with their images.
    /// </summary>
    [Serializable]
    public class TileImageCollection : Collection<TileImages>, ISerializable {
        /// <summary>
        /// Initializes a new instance of the TileImageCollection class.
        /// </summary>
        public TileImageCollection() : base(new List<TileImages>()) {
        }

        /// <summary>
        /// Initializes a new instance of the TileImageCollection class.
        /// </summary>
        /// <param name="info">Serialization informaiton.</param>
        /// <param name="context">Streaming context.</param>
        protected TileImageCollection(SerializationInfo info, StreamingContext context) : this() {
            foreach (TileImages images in info.GetValue("List", typeof(Array)) as TileImages[]) {
                this.Add(images);
            }
        }

        /// <summary>
        /// Searches the collection using the given Predicate.
        /// </summary>
        /// <param name="match">Predicate to use to search the collection.</param>
        /// <returns>True if the predicate finds a match.</returns>
        public TileImages Find(Predicate<TileImages> match) {
            List<TileImages> items = (List<TileImages>)Items;
            return items.Find(match);
        }

        /// <summary>
        /// Sorts the collection alphabetically.
        /// </summary>
        public void Sort() {
            List<TileImages> items = (List<TileImages>)Items;
            items.Sort();
        }

        /// <summary>
        /// Convert this Collection into an Array.
        /// </summary>
        /// <returns>An array of TileImages.</returns>
        public TileImages[] ToArray() {
            List<TileImages> list = (List<TileImages>)this.Items;
            TileImages[] arr;
            arr = list.ToArray();
            return arr;
        }

        /// <summary>
        /// Gets the object data needed to serialize this instance.
        /// </summary>
        /// <param name="info">Serialization information.</param>
        /// <param name="context">Streaming context.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
            info.AddValue("List", this.ToArray(), typeof(Array));
        }
    }
}
